import com.google.gdata.client.projecthosting.*;
import com.google.gdata.data.*;
import com.google.gdata.data.projecthosting.*;
import com.google.gdata.util.ServiceException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.List;
import java.util.Vector;

 
public class os_cal_label 
{

	private String project = "chromium-os";
	
	private Connection conn = null;
    private String url = "jdbc:mysql://localhost:3306/";
    private String dbName = "OSCHROME"; 
    private String driver = "com.mysql.jdbc.Driver";
    private String userName = "root"; 
    private String password = "123";
    private String issue_tab = "os_bugdb2";
    private String write_tabel = "os_milestone_infor";
    private ProjectHostingService myService = null;
	
	private void initdb(){
		 try {
		      Class.forName(driver).newInstance();
		      conn = DriverManager.getConnection(url+dbName,userName,password);
		  
		 } catch (Exception e) {
		      e.printStackTrace();
		 }
	}
	
	private void closedb(){ 
		 try {
		      conn.close();
		    } catch (Exception e) {
		      e.printStackTrace();
		    }
	} 
	
	private void insert(int issueID, String milestone, int movedfrom, String label_str)
	{
		String insertString = "insert into " + write_tabel + " VALUES (" + issueID  + ",' "+milestone+"',"+movedfrom+",'"+label_str+"')";
		//System.out.println(insertString); 
		try {
			Statement stmt = conn.createStatement();
	   		stmt.executeUpdate(insertString);
			stmt.close();
		} catch(Exception e) {
			System.out.println(e);
		}
	}
	
	
	
	
	public void extractInformation(ProjectHostingService service, String issueId){
		

		int movedfrom = 0;
		String labelstr="";
		String mstone = "";	
		
		URL feedUrl;
			try {
				feedUrl = new URL("https://code.google.com/feeds/issues/p/"+project+"/issues/full/" + issueId);
			
			IssuesEntry	entry = service.getEntry(feedUrl, IssuesEntry.class);
			 
			System.out.println("--------------");
			System.out.println("issueid="+issueId);
			Id id = entry.getIssueId();
			int issueID = id.getValue();
			List<Label> labels =  entry.getLabels();
			
			for(int i=0;i<labels.size();i++){
				Label l = labels.get(i);
				//l.getValue();
				System.out.println("i="+labels.get(i));
			    labelstr=labelstr+","+labels.get(i);
				if(labels.get(i).toString().toLowerCase().contains("MovedFrom"))
				{
					interupt();
					movedfrom++;
				}
				if(labels.get(i).toString().toLowerCase().contains("mstone-"))
				{
				    try{
					mstone=labels.get(i).toString().substring(20);
					mstone = mstone.split("}")[0];
					System.out.println("ms="+mstone);
				    }catch(ArrayIndexOutOfBoundsException e)
				    {}
					//interupt();
				}
				else if(labels.get(i).toString().toLowerCase().contains("mstone"))
				{
				
					mstone=labels.get(i).toString().substring(19);
					mstone = mstone.split("}")[0];
					System.out.println("ms="+mstone);
					
					//interupt();
				}
			
			
			
			}
			
			} catch (MalformedURLException e) 
			{ 	e.printStackTrace(); 	} 
			
	        catch(IOException e)
	        { 	e.printStackTrace(); }
	        
	        catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	        
	        insert(Integer.parseInt(issueId), mstone, movedfrom ,labelstr);
		    	
	}
	
	  
	public void process(){
		try{
			myService = new ProjectHostingService("Sample Application");
			//printAllIssues(myService, project);
			
			Vector  issues = new Vector();
			issues = read_issues();
			
			for(int i=0;i<issues.size();i++){
				int issue = (Integer) issues.get(i);
				extractInformation(myService, Integer.toString(issue)); 
				
			}
			
			//specificIssue(myService, "60055");
			//extractInformation(myService, "60055"); 
		}catch(Exception e){
			System.out.println(e);
		}
	}
	
	
	
	private Vector read_issues() 
	{
	
		Statement  stmt = null;
		Vector issue =  new Vector();
		//String insert ="select issueid from "+ issue_tab +"  where issueid =15839";
		//String insert ="select issueid from os_bugdb2 where issueid not in (select issueid from " +
			//	"milestone_infor)";
		String insert ="select issueid from "+ issue_tab +" limit 0, 5000";
		try 
		{
			   stmt = conn.createStatement();
			   stmt.execute(insert);
			   ResultSet rs =  stmt.getResultSet();
			   while(rs.next())
			   {
				   issue.add(rs.getInt("issueid"));
			   }
			   
			   System.out.println("Size="+issue.size());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return issue;
	}

	public static void main(String[] args){
		os_cal_label ite = new os_cal_label();
		ite.initdb();
		ite.process();
		ite.closedb();


	}
	
	
	
	
	public void interupt()
	{
		
		BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Intr");
		try {
			br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
}

